package de.tabuma.wsdl2xform.history.logik;

import de.tabuma.wsdl2xform.logik.*;
import de.tabuma.wsdl2xform.history.entity.JournalEntry;
import javax.jdo.*;
import java.util.logging.*;
import java.util.*;
import java.text.*;

/**
 *
 * @author $Author: alefen $
 * @version $Revision: 1.1 $
 * @since $Date: 2009/07/21 19:37:14 $
 */
public class JournalEntryFacade {

    public static final int ORDER_ITEM = 1;
    public static final int PROJECT_ITEM = 2;
    
    public static final int ACTION_CREATE = 1;
    public static final int ACTION_UPDATE = 2;
    public static final int ACTION_REMOVE = 3;
    public static final int ACTION_CANCEL = 4;
    public static final int ACTION_ELEMENT_ADD = 5;
    public static final int ACTION_ELEMENT_REMOVE = 6;
    public static final int ACTION_ELEMENT_UPDATE = 7;

    private static Logger log = Logger.getLogger(JournalEntryFacade.class.getName());

    public static void createJournalEntry(int itemTyp,int itemId,String user,int action,int parameterId,String parameterValue) {
        PersistenceManager pm = PMF.get().getPersistenceManager();
        JournalEntry journalEntry = new JournalEntry(itemTyp, itemId, user, action, parameterId, parameterValue);
        pm.makePersistent(journalEntry);
        pm.close();
    }
    public static void createJournalEntry(int itemTyp,int itemId,String user,int action,int parameterId,Date parameterValueDate) {
        PersistenceManager pm = PMF.get().getPersistenceManager();
        JournalEntry journalEntry = new JournalEntry(itemTyp, itemId, user, action, parameterId, parameterValueDate);
        pm.makePersistent(journalEntry);
        pm.close();
    }
    public static void createJournalEntry(int itemTyp,int itemId,String user,int action,int parameterId,List parameterValueDate) {
        PersistenceManager pm = PMF.get().getPersistenceManager();
        JournalEntry journalEntry = new JournalEntry(itemTyp, itemId, user, action, parameterId, parameterValueDate);
        pm.makePersistent(journalEntry);
        pm.close();
    }
    public static void createJournalEntry(int itemTyp,int itemId,String user,int action,int parameterId,Long parameterValueDate) {
        PersistenceManager pm = PMF.get().getPersistenceManager();
        JournalEntry journalEntry = new JournalEntry(itemTyp, itemId, user, action, parameterId, parameterValueDate);
        pm.makePersistent(journalEntry);
        pm.close();
    }
    public static void createJournalEntry(int itemTyp,int itemId,String user,int action,int parameterId,Double parameterValueDate) {
        PersistenceManager pm = PMF.get().getPersistenceManager();
        JournalEntry journalEntry = new JournalEntry(itemTyp, itemId, user, action, parameterId, parameterValueDate);
        pm.makePersistent(journalEntry);
        pm.close();
    }
    public static void createJournalEntry(int itemTyp,int itemId,String user,int action,int parameterId,Boolean parameterValueBoolean) {
        PersistenceManager pm = PMF.get().getPersistenceManager();
        JournalEntry journalEntry = new JournalEntry(itemTyp, itemId, user, action, parameterId, parameterValueBoolean);
        pm.makePersistent(journalEntry);
        pm.close();
    }

    public static List<JournalEntry> getJournalEntryList(int itemTyp,int itemId) {
        String filter = "itemTyp == "+itemTyp+" && itemId == "+itemId;
        return getListWithFilter(filter);
    }

    public static List<JournalEntry> getJournalEntryList(String user) {
        String filter = "user == '"+user+"'";
        return getListWithFilter(filter);
    }

    private static List<JournalEntry> getListWithFilter(String filter) {
        ArrayList<JournalEntry> journalEntryList = new ArrayList<JournalEntry>();
        PersistenceManager pm = PMF.get().getPersistenceManager();
        Query query = pm.newQuery(JournalEntry.class);
        query.setFilter(filter);
        log.fine("Get list with filter:"+filter);
        List<JournalEntry> list = (List<JournalEntry>)query.execute();
        Iterator<JournalEntry> journalEntryIterator = list.iterator();
        while(journalEntryIterator.hasNext()) {
            journalEntryList.add(journalEntryIterator.next());
        }
        query.closeAll();
        pm.close();
        log.fine(journalEntryList.toString());
        return journalEntryList;
    }
}
